<template>
  <a-modal :footer="false" v-model:open="webConfig.isOpenLogin" title="登录/注册" @ok="handleOk">
    <div v-if="status" class="login">
      <a-form
          :model="formState"
          name="normal_login"
          class="login-form"
          @finish="onFinish"
          v-bind="layout"
          @finishFailed="onFinishFailed"
      >
        <a-form-item
            label="账号"
            name="username"
            :rules="[{ required: true, message: 'Please input your username!' }]"
        >
          <a-input v-model:value="formState.username">
            <template #prefix>
              <UserOutlined class="site-form-item-icon" />
            </template>
          </a-input>
        </a-form-item>

        <a-form-item
            label="密码"
            name="password"
            :rules="[{ required: true, message: 'Please input your password!' }]"
        >
          <a-input-password v-model:value="formState.password">
            <template #prefix>
              <LockOutlined class="site-form-item-icon" />
            </template>
          </a-input-password>
        </a-form-item>

        <a-form-item>
          <a class="login-form-forgot" href="">忘记密码?</a>
        </a-form-item>

        <a-form-item>
          <a-button :loading="loading"  :disabled="disabled" type="primary" html-type="submit" class="login-form-button">
            登录
          </a-button>
          Or
          <a @click="changeStatus(false)" >注册</a>
        </a-form-item>
      </a-form>
    </div>
    <div v-else class="reggie">
      <register @change-status="changeStatus(true)"  />
    </div>
  </a-modal>
</template>
<script lang="ts" setup>
import {reactive, computed, ref} from 'vue';
import { UserOutlined, LockOutlined } from '@ant-design/icons-vue';
import Register from "@/components/Register/index.vue";
import useStore from "@/store/index"
const {user,webConfig} =  useStore();
import {message} from "ant-design-vue";

interface FormState {
  username: string;
  password: string;
}
const status = ref(true)
const formState = reactive<FormState>({
  username: '',
  password: '',
});
const layout = {
  labelCol: { span: 4 },
  wrapperCol: { span: 14 },
};
const loading = ref(false)
const disabled = computed(() => {
  return !(formState.username && formState.password);
});
const changeStatus = (v : boolean) =>{
  status.value = v
}

const handleOk = (e: MouseEvent) => {
 // console.log(e);
  open.value = false;
};

/**
 * 登录
 * @param values
 */
const onFinish = (values: FormState) => {
  loading.value = true;
  user.Login(values).then(res => {
    message.success("登录成功！")
    location.reload();
  }).catch(err => {
    //登录失败
  }).finally(() => {
    loading.value = false;
  })
};

const onFinishFailed = (errorInfo: any) => {
  console.log('Failed:', errorInfo);
};




</script>
<style scoped>
#components-form-demo-normal-login .login-form {
  max-width: 300px;
}
#components-form-demo-normal-login .login-form-forgot {
  float: right;
}
#components-form-demo-normal-login .login-form-button {
  width: 100%;
}
</style>
